import asyncio
from edge_tts import Communicate

async def text_to_speech(text, voice, output_file):
    communicate = Communicate(text, voice)
    with open(output_file, "wb") as file:
        async for chunk in communicate.stream():
            if chunk["type"] == "audio":
                file.write(chunk["data"])

def async_text_to_speech(text, voice, output_file):
    asyncio.run(text_to_speech(text, voice, output_file))


if __name__ == "__main__":
    # print(asyncio.run(edge_tts.list_voices()))
    asyncio.run(text_to_speech(
        "使用这些库，您可以构建一个流程，首先将文字转换成语音，然后将语音识别成文字，并最终生成字幕文件。以下是一个简化的流程示例",
        "zh-CN-YunyangNeural", "output.mp3"))
